import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;

public class Result extends JPanel
{
    public Result(Patient patient)
    {
        super(new BorderLayout());
        isSaved = false;
        d = patient.getDiagnosticList();
        index = patient.getIndexCheckup();
        textAreaDiagnostic = new JTextPane();
        Border etched = BorderFactory.createEmptyBorder();
        Border titled = BorderFactory.createTitledBorder(
            etched, 
            Resources.getString("result"),
            TitledBorder.LEFT, 
            TitledBorder.TOP, 
            Resources.getFont(Resources.FONT_TYPE.BORDER)
        );
        setBorder(titled);
        JPanel panelWest = new JPanel(new BorderLayout());
        JPanel panel_aux = new JPanel(new GridLayout(8,2,15,0));
        JLabel patientNameLabel = Components.getLabel(Resources.getString("name"));
        JLabel patientEndoLabel = Components.getLabel(Resources.getString("endomorphy"));
        JLabel patientMesoLabel = Components.getLabel(Resources.getString("mesomorphy"));
        JLabel patientEctoLabel = Components.getLabel(Resources.getString("ectomorphy"));
        
        JLabel sportNameLabel = Components.getLabel(Resources.getString("sport"));
        JLabel sportEndoLabel = Components.getLabel(Resources.getString("endomorphy"));
        JLabel sportMesoLabel = Components.getLabel(Resources.getString("mesomorphy"));
        JLabel sportEctoLabel = Components.getLabel(Resources.getString("ectomorphy"));
        
        patientNameField = Components.getTextField(false);
        patientEndoField = Components.getTextField(false);
        patientMesoField = Components.getTextField(false);
        patientEctoField = Components.getTextField(false);

        sportNameField = Components.getTextField(false);
        sportEndoField = Components.getTextField(false);
        sportMesoField = Components.getTextField(false);
        sportEctoField = Components.getTextField(false);
        
        panel_aux.add(patientNameLabel);
        panel_aux.add(patientNameField);
        panel_aux.add(patientEndoLabel);
        panel_aux.add(patientEndoField);
        panel_aux.add(patientMesoLabel);
        panel_aux.add(patientMesoField);
        panel_aux.add(patientEctoLabel);
        panel_aux.add(patientEctoField);
        panel_aux.add(sportNameLabel);
        panel_aux.add(sportNameField);
        panel_aux.add(sportEndoLabel);
        panel_aux.add(sportEndoField);
        panel_aux.add(sportMesoLabel);
        panel_aux.add(sportMesoField);
        panel_aux.add(sportEctoLabel);
        panel_aux.add(sportEctoField);
        panelWest.add(panel_aux,BorderLayout.NORTH);
        JPanel panel_aux1 = new JPanel();
        etched = BorderFactory.createEtchedBorder();
        titled = BorderFactory.createTitledBorder(
            etched, 
            Resources.getString("somatochart"),
            TitledBorder.CENTER, 
            TitledBorder.TOP, 
            Resources.getFont(Resources.FONT_TYPE.BORDER)
        );
        panel_aux1.setBorder(titled);
        textPane = new JTextPane();
        textPane.setEditable(false);
        update(patient);
        panel_aux1.add(textPane);
        add(panel_aux1,BorderLayout.CENTER);
        
        JPanel panel_aux2 = new JPanel(new BorderLayout());
        panel_aux2.setPreferredSize(new Dimension(200,200));
        etched = BorderFactory.createEtchedBorder();
        titled = BorderFactory.createTitledBorder(
            etched, 
            Resources.getString("diagnostic"),
            TitledBorder.CENTER, 
            TitledBorder.TOP, 
            Resources.getFont(Resources.FONT_TYPE.BORDER)
        );
        panel_aux2.setBorder(titled);
        
        textAreaDiagnostic.setText(patient.getDianostic());
        StyledDocument doc = textAreaDiagnostic.getStyledDocument();
        SimpleAttributeSet attrs = new SimpleAttributeSet();
        StyleConstants.setFontFamily(attrs,Font.SANS_SERIF);
        StyleConstants.setFontSize(attrs,12);
        StyleConstants.setAlignment(attrs, StyleConstants.ALIGN_LEFT);
        doc.setParagraphAttributes(
                doc.getLength(),
                doc.getLength(),
                attrs,
                false);
        JScrollPane scroll = new JScrollPane(
                textAreaDiagnostic,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scroll.setBounds(5,10,300,300);
        panel_aux2.add(scroll,BorderLayout.CENTER);
        panel_aux2.add(getConfirmPanel(),BorderLayout.SOUTH);
        panelWest.add(panel_aux2,BorderLayout.CENTER);
        add(panelWest,BorderLayout.WEST);
    }
    public void update(Patient patient)
    {
    	index = patient.getIndexCheckup();
    	textAreaDiagnostic.selectAll();
    	textAreaDiagnostic.cut();
    	textAreaDiagnostic.setText(d.get(index).getDianostic());
        textPane.selectAll();
        textPane.cut();
        Somatotype sportSoma = SomatotypeIO.getSomatotype( patient.getSport(), patient.getGender());
        double endomorphy = NutritionForm.endomorphy(
            patient.getAnthropometryValue(ID.A.SIZE),
            patient.getAnthropometryValue(ID.A_CREASE.TRICEP),
            patient.getAnthropometryValue(ID.A_CREASE.SUBSCAPULARIS),
            patient.getAnthropometryValue(ID.A_CREASE.SUPRAILIAC)
        );
        double mesomorphy = NutritionForm.mesomorphy(
            patient.getAnthropometryValue(ID.A_DIAMETER.HUMERAL),
            patient.getAnthropometryValue(ID.A_DIAMETER.FEMORAL),
            patient.getAnthropometryValue(ID.A_GIRTH.CONTRACTED_ARM),
            patient.getAnthropometryValue(ID.A_GIRTH.LEG),
            patient.getAnthropometryValue(ID.A.SIZE),
            patient.getAnthropometryValue(ID.A_CREASE.TRICEP),
            patient.getAnthropometryValue(ID.A_CREASE.LEG)
        );
        double ectomorphy = NutritionForm.ectomorphy(
            patient.getAnthropometryValue(ID.A.SIZE),
            patient.getAnthropometryValue(ID.A.CURRRENT_WEIGHT)
        );
        Somatochart somatochart = new Somatochart();
        somatochart.addPoint(sportSoma.getEndo(),sportSoma.getMeso(),sportSoma.getEcto(),patient.getSport());
        somatochart.addPoint(endomorphy,mesomorphy,ectomorphy,patient.getName());
        textPane.insertIcon(new ImageIcon(somatochart.getImage()));
        
        patientNameField.setText(patient.getName());
        if(patient.getAnthropometryValue(ID.A.SIZE) == 0)
        {
        	patientEndoField.setEnabled(false);
        	patientEndoField.setText((new Double(Methods.round(endomorphy,3))).toString());
        }
        else
        {
            patientEndoField.setEnabled(true);
            patientEndoField.setText((new Double(Methods.round(endomorphy,3))).toString());
        }
        patientMesoField.setText((new Double(Methods.round(mesomorphy,3))).toString());
        if(patient.getAnthropometryValue(ID.A.CURRRENT_WEIGHT) == 0)
        {
        	patientEctoField.setEnabled(false);
        	patientEctoField.setText((new Double(Methods.round(ectomorphy,3))).toString());
        }
        else
        {
            patientEctoField.setEnabled(true);
            patientEctoField.setText((new Double(Methods.round(ectomorphy,3))).toString());
        }
            
        sportNameField.setText(patient.getSport());
        sportEndoField.setText((new Double(Methods.round(sportSoma.getEndo(),3))).toString());
        sportMesoField.setText((new Double(Methods.round(sportSoma.getMeso(),3))).toString());
        sportEctoField.setText((new Double(Methods.round(sportSoma.getEcto(),3))).toString());
    }
    private JPanel getConfirmPanel()
    {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER,30,15));
        JButton okButton = new JButton(Resources.getString("ok"));
        okButton.setFont(Resources.getFont(Resources.FONT_TYPE.LABELS));
        okButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                	confirmNewValues();
                	isSaved = false;
                }
            }
        );
        JButton cancelButton = new JButton(Resources.getString("cancel"));
        cancelButton.setFont(Resources.getFont(Resources.FONT_TYPE.LABELS));
        cancelButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    textAreaDiagnostic.setText(d.get(index).getDianostic());
                }
            }
        );
        JPanel panel_aux1 = new JPanel(new GridLayout(1,2,30,0));
        panel_aux1.add(okButton);
        panel_aux1.add(cancelButton);
        panel.add(panel_aux1);
        return panel;
    }
    public String getDiagnostic()
    {
        return textAreaDiagnostic.getText();
    }
    public ArrayList<Diagnostic> getDiagnosticList()
    {
        return d;
    }
    public void confirmNewValues()
    {
    	d.get(index).setDiagnostic(getDiagnostic());
    }
    public boolean existNewInfo()
    {
    	if(!textAreaDiagnostic.getText().equals(d.get(index).getDianostic()))
    		return true;
    	return false;
    }
    public void setSavedState(boolean isSaved_)
    {
    	isSaved = isSaved_;
    }
    public boolean getSavedState()
    {
    	return isSaved;
    }
    private boolean isSaved;
    private ArrayList<Diagnostic> d;
    private int index;
    //Patient fields
    private JTextPane textPane;
    private JTextField patientNameField;
    private JTextField patientEndoField;
    private JTextField patientMesoField;
    private JTextField patientEctoField;
    //Sport fields
    private JTextField sportNameField;
    private JTextField sportEndoField;
    private JTextField sportMesoField;
    private JTextField sportEctoField;
    
    private JTextPane textAreaDiagnostic;
}
